/**
 * author:Administrator
 * date:2016/2/26
 * desc: 消费者 command 接口
 */

"use strict";

let Customer = require('../domain/model_cmd/customer/Customer.js');
let CustomerRepo = require('../domain/model_cmd/customer/CustomerRepo.js');
let Order = require('../domain/model_cmd/order/Order.js');
let OrderRepo = require('../domain/model_cmd/order/OrderRepo.js');
let Ware = require('../domain/model_cmd/ware/Ware.js');
let WareRepo = require('../domain/model_cmd/ware/WareRepo.js');

let CouponLog = require('../domain/model_cmd/couponLog/CouponLog.js');
let CouponLogRepo = require('../domain/model_cmd/couponLog/couponLogRepo.js');
let CouponMjs = require('../domain/model_cmd/coupon/CouponMjs.js');
let CouponRepo = require('../domain/model_cmd/coupon/couponRepo.js');

let Ticket = require('../domain/model_cmd/ticket/Ticket.js');
let TicketRepo = require('../domain/model_cmd/ticket/TicketRepo.js');

let CouponApplySaga = require('../domain/saga/').CouponApplySaga;
let OrderCreatedSaga = require('../domain/saga/').OrderCreatedSaga;
let CouponUseSaga = require('../domain/saga/').CouponUseSaga;
let OrderPaySaga = require('../domain/saga/').OrderPaySaga;

let service = {};
module.exports = service;

/**
 * 申请一个 coupon
 * @param customerId
 * @param couponId
 */
service.applyCoupon = function * (customerId, couponId) {
  let customer = yield CustomerRepo.find(customerId);
  if (!customer) return false;

  let couponPojo = yield CouponRepo.find(couponId);
  if (!couponPojo) return false;
  let coupon = CouponMjs.rebuild(couponPojo);
  if (!coupon.checkApplyLimit()) {
    return false;
  }

  let couponApplySaga = new CouponApplySaga(customerId, coupon);
  yield couponApplySaga.start();
  let result = yield couponApplySaga.result();
  console.log(result);
  return result;
};


/**
 * 添加一个未支付订单
 * @param customerId
 * @param wareId
 * @param count
 * TODO => placeOrder
 */
service.addUnpaidOrder = function * (customerId, wareId, count) {
  if (count <= 0) return false;

  let customer = yield CustomerRepo.find(customerId);
  if (!customer) return false;

  let warePojo = yield WareRepo.find(wareId);
  if (!warePojo) return false;
  let ware = Ware.rebuild(warePojo);
  if (ware.remain < count) return false;        // 货物存量不足
  let snapshot = yield ware.getSnapshot();

  // saga
  let orderCreateSaga = new OrderCreatedSaga(customerId, snapshot, count);
  yield orderCreateSaga.start();
  let result = yield orderCreateSaga.result();
  console.log(result);

  return result;
};

/**
 * 对一个订单使用 优惠券
 * 订单支付涉及到多个 domainObject
 * order 本身
 * coupon 检测使用策略
 * couponLog 检测是否已用
 */
service.orderUseCoupon = function*(customerId, orderId, couponLogId) {
  let orderPojo = yield OrderRepo.find(orderId);
  if (!orderPojo) return false;                                             // order 无效
  if (orderPojo.couponLogId) return false;                                 // order已经绑定了优惠券
  let order = Order.rebuild(orderPojo);

  let couponLogPojo = yield CouponLogRepo.find(couponLogId);
  if (!couponLogPojo) return false;                                    // couponLog 无效
  let couponLog = CouponLog.rebuild(couponLogPojo);
  if (couponLog.isUsed()) return false;                                // couponLog 已经使用

  // 检测
  let couponPojo = yield CouponRepo.find(couponLog.couponId);
  let coupon = CouponMjs.rebuild(couponPojo);
  if (!coupon.checkUseLimit(order)) return false;                           // coupon不能使用在 这个order上

  // 检测通过: 标记couponLog 已用,修改order
  let couponUseSaga = new CouponUseSaga(couponLog, order);
  yield couponUseSaga.start();
  let result = yield couponUseSaga.result();
  console.log(result);
};

/**
 * 支付一个订单, 生成一个(?多个)ticket
 * 允许order.customerId != customerId,
 * 即允许支付不是自己的订单，代付
 * @param orderId
 */
service.payOrder = function * (orderId) {
  let orderPojo = yield OrderRepo.find(orderId);
  if (!orderPojo) return false;
  let order = Order.rebuild(orderPojo);
  if (!order.canPay()) return false;

  let orderPaySaga = new OrderPaySaga(order);
  yield orderPaySaga.start();
  let result = yield orderPaySaga.result();
  console.log(result);
};

/**
 * 取消一个未支付订单, 需要修改 商品数量
 * @param customerId
 * @param orderId
 */
service.cancelOrder = function*(customerId, orderId) {
  let order = yield OrderRepo.find(orderId);
  if (!order) return false;
  if (order.customerId != customerId) return false;

  yield order.cancel();
};

/**
 * ticket 申请退款
 */
service.ticketDrawback = function*() {

};